class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
         val = x;
       next = null;
    }
}



public class GetIntersectionNode {
    public ListNode getIntetsectionNode(ListNode headA,ListNode headB) {
        ListNode p1=headA;
        ListNode p2=headB;
        int l1=0;
        int l2=0;
        while(p1!=null){
            l1++;
            p1=p1.next;
        }
        while(p2!=null){
            l2++;
            p2=p2.next;
        }
        p1=headA;
        p2=headB;
        int lenth=l1-l2;
        if(lenth>0){
            while(lenth!=0) {
                p1 = p1.next;
                lenth--;
            }
        }else{
                while(lenth!=0){
                    p2=p2.next;
                    lenth++;
                }
            }
        while(p1!=p2){
            p1=p1.next;
            p2=p2.next;
        }
        return p1;
    }
}